package com.ffdms.mobileoffice;

import java.util.List;

import com.ffdms.service.MessageService;
import com.ffdms.util.Constant;
import com.ffdms.vo.Message;

import android.os.Bundle;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
	private TabHost tab;
	private LayoutInflater layoutInflater;
	// tab
	private TabHost mTabHost;
	private TabSpec mNews;
	private Intent mIntent;
	private TextView mTextView;
	private View mView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabwidget);
		Init();
		// getlist();
	}

	private void getlist() {
		MessageService msgSvc = new MessageService(this, null);
		List<Message> list = msgSvc.getAllMessages();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// startService(new Intent(this, MessagerService.class));
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		// stopService(new Intent(this, MessagerService.class));
	}

	private void addTab(String tag, Class classname, int imgid, String text) {
		mView = LayoutInflater.from(this).inflate(R.layout.main_custom_tab,
				null);
		((ImageView) mView.findViewById(R.id.tab_icon)).setImageResource(imgid);
		mTextView = (TextView) mView.findViewById(R.id.tab_title);
		mTextView.setText(text);
		mIntent = new Intent().setClass(this, classname);
		mNews = mTabHost.newTabSpec(tag).setIndicator(mView)
				.setContent(mIntent);
		mTabHost.addTab(mNews);
	}

	public void Init() {
		mTabHost = getTabHost();
		layoutInflater = LayoutInflater.from(this);
		int count = Constant.ConValue.mTabClassArray.length;
		for (int i = 0; i < count; i++) {
			addTab(Constant.ConValue.mTextviewArray[i],
					Constant.ConValue.mTabClassArray[i],
					Constant.ConValue.mImageViewArray[i],
					Constant.ConValue.mTextviewArray[i]);
			// TabSpec tabSpec = tab
			// .newTabSpec(Constant.ConValue.mTextviewArray[i])
			// .setIndicator(getTabItemView(i))
			// .setContent(getTabItemIntent(i));
			// tab.addTab(tabSpec);
			//
			// tab.getTabWidget().getChildAt(i)
			// .setBackgroundResource(R.drawable.selector_tab_background);
		}
	}

	private View getTabItemView(int index) {
		// �����µ���Դ
		View view = layoutInflater.inflate(R.layout.tab_item_view, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		System.out.println("11---------" + index);
		if (imageView != null) {
			imageView
					.setImageResource(Constant.ConValue.mImageViewArray[index]);
		}
		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(Constant.ConValue.mTextviewArray[index]);

		return view;

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_setting:
			Intent t = new Intent(MainActivity.this, SettingActivity.class);
			startActivity(t);
			break;
		case R.id.menu_exit:
			showExitDialog();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private Intent getTabItemIntent(int index) {
		Intent intent = new Intent(this,
				Constant.ConValue.mTabClassArray[index]);

		return intent;
	}

	protected void showExitDialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确定要退出系统吗?");
		builder.setTitle("系统提醒");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						final SharedPreferences sp = getSharedPreferences(
								"login", Context.MODE_PRIVATE);
						Editor edi = sp.edit();
						edi.putString("Id", "");
						edi.putString("Password", "");
						edi.commit();
						dialog.dismiss();
						Intent startMain = new Intent(Intent.ACTION_MAIN);
						startMain.addCategory(Intent.CATEGORY_HOME);
						startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(startMain);
						System.exit(0);
					}
				});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

}
